package com.controller;

import com.entity.Employee;
import com.service.IEmployeeService;
import com.vo.employeeVo.EmployeeListRequestVo;
import com.vo.employeeVo.EmployeeListResponseVo;
import com.vo.employeeVo.addEmployeeVo;
import com.vo.employeeVo.updateEmployeeVo;
import com.vo.productVo.addProductVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @Describe 员工信息管理控制器
 */
@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Resource
    IEmployeeService employeeService;
    @Resource
    HttpServletRequest request;

    /**
     * 获取员工信息列表
     * @return
     */
    @RequestMapping("/getEmployeeList")
    @ResponseBody
    public List<EmployeeListResponseVo> getEmployeeList(EmployeeListRequestVo vo){
        Object session= request.getSession().getAttribute("user");
        if(session != null) {
            return employeeService.getEmployeeList(vo);
        }
        return null;
    }

    /**
     * 修改员工信息（薪资，配送区域，员工照片）
     */
    @PutMapping("/updateEmployee")
    @ResponseBody
    public void updateEmployee(@RequestBody updateEmployeeVo vo){
        Object session = request.getSession().getAttribute("user");
        if (session != null) {
            employeeService.updateEmployee(vo);
        }
    }

    /**
     * 添加新员工
     */
    @RequestMapping("/addEmployee")
    @ResponseBody
    public String addEmployee(addEmployeeVo addVo, MultipartFile file) throws IOException {
        String filename = UUID.randomUUID() + "_" + file.getOriginalFilename();
        File dirs = new File("src/main/resources/static/images/employee/"+filename);
        if (file.isEmpty()){
            employeeService.addEmployee(addVo);
            return "图片上传失败！";
        }else {
            file.transferTo(new File(dirs.getAbsolutePath()));
            addVo.setImage("/static/images/employee/"+filename);
            employeeService.addEmployee(addVo);
            return "添加成功!";
        }
    }


    /**
     * 每月清除员工评价记录（总评价数不清除）
     */
    @RequestMapping("/cleanEmployeeReview")
    @ResponseBody
    public Object updateEmployeeReview(){
        return null;
    }
}
